from django import http
from django.db import DatabaseError
from django.http import HttpResponseBadRequest
from django.shortcuts import render, redirect

# Create your views here.
from django.urls import reverse
from django.views import View

from apps.comments.models import Comments


class CommentsView(View):
    """comments function"""

    def post(self,request,id):

        comment_content=request.POST.get('comment')

        comment_article_id=id

        comment_user_id = request.user.id

        try:
            Comments.objects.create(content=comment_content,comment_article_id=comment_article_id,comment_user_id=comment_user_id)

        except DatabaseError:
            return HttpResponseBadRequest('评论失败,请重试')

        return redirect(reverse('category:article_detail',kwargs={'id':id}))






